//
//  MessageTableViewController.swift
//  RoundCatPlay2
//
//  Created by 姜澎 on 2019/10/25.
//  Copyright © 2019 jiangpeng. All rights reserved.
//

import UIKit

class MessageTableViewController: UITableViewController {
    
    var tableSourceAr = [
        [
            ["titleImage":"gfxx","title":"官方通知","conten":"嗨哥们,我是官方专业陪玩代...","data":"昨天","mn":"3"],["titleImage":"kf","title":"我的客服","conten":"嗨狗电竞竭诚为您服务","data":"5月15","mn":"8"]
        ],
        
        [
                ["titleImage":"dm","title":"呆妹儿","conten":"小哥哥快来呀~一起玩哦！","data":"5月12","mn":"6"],
                ["titleImage":"cjzb","title":"吃鸡主播~","conten":"吃鸡带你飞哦小哥哥，快来！","data":"5月10","mn":""],
                ["titleImage":"tm","title":"冯提莫小姐姐","conten":"是谁的心啊~孤单的流下，他还...","data":"4月30","mn":"2"],
                ["titleImage":"yt","title":"樱桃小姐姐","conten":"喜欢玩英雄联盟，我喜欢玩亚瑟！","data":"4月28","mn":"6"],
                ["titleImage":"nj","title":"你家的大帅哥","conten":"关于本次会议，领导提出重要指示...","data":"4月28","mn":""]
        ]
    ]
    
    private lazy var titleLabel: UILabel = {
        let l = UILabel()
        l.text = "消息"
        l.font = UIFont.boldSystemFont(ofSize: 22)
        return l
    }()
    
    private var searchBar: UISearchBar = {
        var search = UISearchBar(frame: CGRect(x: 0, y: 0, width:Int(UIScreen.main.bounds.size.width) - 100 , height: 20))
        search.tintColor = UIColor.gray
        search.placeholder = "搜索关键词"
        let seachTextFeild = search.subviews.first?.subviews.last
        seachTextFeild?.backgroundColor = UIColor(red: 239/255, green: 239/255, blue: 239/255, alpha: 1)
        return search
    }()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.navigationController?.navigationBar.shadowImage = UIImage()
        
        self.navigationController?.navigationBar.barTintColor = .white
        navigationItem.leftBarButtonItem = UIBarButtonItem(customView: self.titleLabel)
        navigationItem.rightBarButtonItem = UIBarButtonItem(customView: self.searchBar)
        
        self.tableView.register(MessageTableViewCell.self, forCellReuseIdentifier: "messageCell")
        self.tableView.showsVerticalScrollIndicator = false
        self.tableView.separatorStyle = .none
    }
    
    
}


// MARK: - Table view data source
extension MessageTableViewController {
    
    
    override func numberOfSections(in tableView: UITableView) -> Int {
        
        return self.tableSourceAr.count
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return self.tableSourceAr[section].count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "messageCell") as? MessageTableViewCell
        cell?.selectionStyle = .none
        let arr = self.tableSourceAr[indexPath.section]
        let dic = arr[indexPath.row]
        let m = MessageModel(titleImageName: dic["titleImage"]!, contentTitle: dic["conten"]!, title: dic["title"]!, date: dic["data"]!, messageNum: dic["mn"]!)
        
        cell?.messageModel = m
        
        if cell?.MessageNumberL.text! == "" {
            
            cell?.MessageNumberL.isHidden = true
        }else {
            
            cell?.MessageNumberL.isHidden = false
        }
        self.tableView?.tableFooterView = UIView()
        return cell!
    }
    
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 70
    }
    
    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        if section == 1 {
            return 15
        }
        return 0
    }
    
    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let v = UIView()
        v.backgroundColor = UIColor(red: 246/255, green: 246/255, blue: 246/255, alpha: 1)
        return v
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        self.navigationController?.pushViewController(LianTianLieBiaoViewController(), animated: true)
    }
}
